package com.ltmonitor.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ltmonitor.dao.IBaseIbatisDao;
import com.ltmonitor.service.impl.QueryService;
import com.ltmonitor.util.DateUtil;
import com.ltmonitor.web.util.JsonMessage;
import com.ltmonitor.web.util.SpringApplicationContextHolder;

import net.sf.jsqlparser.statement.select.Limit;

@Service("updateDriverCardInfoService")
public class UpdateDriverCardInfoService  implements IUpdateDriverCardInfoService {

	//定义查询服务对象
	private IQueryService queryService = null;
	
	
	/**
	 * 更新插拔卡记录中的图片信息
	 * @param hours 小时数
	 * @param limit 每次更新最大个数
	 * @return
	 */
	public JsonMessage updatePicInfo(int hours ,int limit)
	{
		
		JsonMessage result = null;
		int updateNum = -1;
		try {
			
			if(queryService == null) {
				queryService = SpringApplicationContextHolder.getBean("queryService");
			}
			if(queryService == null) {
				result = new JsonMessage(true, "数据库查询对象为空");
			}
			
			Map<String, Object> map1 = new HashMap();
			Date date = new Date();
			date.setTime(date.getTime() - 1000 * 60 * 60 * hours );
			map1.put("startTime", DateUtil.datetimeToString(date));
			map1.put("limit", limit);
			
			List fileList = null;
			
			//查找未更新数据
			String queryId = "driverCardRecord.selectUnUpdatedDatas";
			List cmdIdList = queryService.query(queryId, map1);
//			System.out.println("查询到数据数量为 ： " + cmdIdList.size());
			if(cmdIdList != null && cmdIdList.size() > 0) {
				Map<String, Object> map = new HashMap<>();
				//开始转换列表
				List listTemp = new ArrayList<>();
				for(Object obj:cmdIdList) {
					Map<String, Object> mapTemp = (Map<String, Object>) obj;
					listTemp.add((int)mapTemp.get("cmdId"));
				}
				
				map.put("cmdIdList", listTemp);
				queryId = "driverCardRecord.selectDriverCardCmdResultByCmdId";
				fileList = queryService.query(queryId, map);
			}
			
			//开始更新数据
			if(fileList != null && fileList.size() > 0) {
				updateNum = 0;
				Map<String, Object> mapTemp = null;
				for(int i=0 ; i<fileList.size() ; i++) {
					mapTemp = (Map<String, Object>) fileList.get(i);
					String fileName = mapTemp.get("fileName").toString();
					if(fileName != "") {
//						int cmdId = (int) mapTemp.get("commandId");
//						String updateDate = mapTemp.get("sendTime").toString();
						queryId = "driverCardRecord.updateDriverCardAttachInfo";
						queryService.query(queryId, mapTemp);
						updateNum  ++;
					}
				}
				result = new JsonMessage(true, updateNum);
			}

			result = new JsonMessage(true, updateNum);
		} catch (Exception e) {
			// TODO: handle exception
			result = new JsonMessage(false, e.getMessage());
//			System.out.println("++++++++++++++++++++++++");
//			System.out.println("++++++++++++++++++++++++");
//			System.out.println("++++++++++++++++++++++++");
//			System.out.println("++++++++++++++++++++++++");
			
			e.printStackTrace();
		}
		return result;
	}

	
}
